using UnityEngine;
using System.Collections;

[ExecuteInEditMode]
public class lwGUI9Box : MonoBehaviour
{
	public PackedSprite[] m_nineparts;
	// nine parts indices
	//0|1|2
	//3|4|5
	//6|7|8

	public float m_fWidth = 120;
	public float m_fHeight = 140;
	public float m_fBorder = 4;


	// Use this for initialization
	void Start ()
	{
		this.SetSize( m_fWidth, m_fHeight, m_fBorder );
#if UNITY_EDITOR
		m_fPrevWidth = m_fWidth;
		m_fPrevHeight = m_fHeight;
		m_fPrevBorder = m_fBorder;
#endif //UNITY_EDITOR
	}

#if UNITY_EDITOR
	private float m_fPrevWidth;
	private float m_fPrevHeight;
	private float m_fPrevBorder;

	void Update()
	{
		if(m_fPrevWidth != m_fWidth || m_fPrevHeight != m_fHeight || m_fPrevBorder != m_fBorder)
			this.SetSize( m_fWidth, m_fHeight, m_fBorder );
		m_fPrevWidth = m_fWidth;
		m_fPrevHeight = m_fHeight;
		m_fPrevBorder = m_fBorder;
	}
#endif //UNITY_EDITOR
	
	public void SetSize(float fDx, float fDy, float fBorder)
	{
		m_fWidth = fDx;
		m_fHeight = fDy;
		m_fBorder = fBorder;
		
		if( m_nineparts != null )
		{
//			if( m_nineparts[0] != null )
//			{
				m_nineparts[0].SetSize( m_fBorder, m_fBorder );
				m_nineparts[0].transform.localPosition = new Vector3( -m_fWidth * 0.5f, m_fHeight * 0.5f, 0);
				m_nineparts[0].anchor = SpriteRoot.ANCHOR_METHOD.BOTTOM_RIGHT;
//			}
//			if( m_nineparts[1] != null )
//			{
				m_nineparts[1].SetSize( m_fWidth, m_fBorder );
				m_nineparts[1].transform.localPosition = new Vector3(0, m_fHeight * 0.5f, 0);
				m_nineparts[1].anchor = SpriteRoot.ANCHOR_METHOD.BOTTOM_CENTER;
//			}
//			if( m_nineparts[2] != null )
//			{
				m_nineparts[2].SetSize( m_fBorder, m_fBorder );
				m_nineparts[2].transform.localPosition = new Vector3(m_fWidth * 0.5f, m_fHeight * 0.5f, 0);
				m_nineparts[2].anchor = SpriteRoot.ANCHOR_METHOD.BOTTOM_LEFT;
//			}
//			if( m_nineparts[3] != null )
//			{
				m_nineparts[3].SetSize( m_fBorder, m_fHeight );
				m_nineparts[3].transform.localPosition = new Vector3(-m_fWidth * 0.5f, 0, 0);
				m_nineparts[3].anchor = SpriteRoot.ANCHOR_METHOD.MIDDLE_RIGHT;
//			}
//			if( m_nineparts[4] != null )
				m_nineparts[4].SetSize( fDx, fDy );
				m_nineparts[4].anchor = SpriteRoot.ANCHOR_METHOD.MIDDLE_CENTER;
//			if( m_nineparts[5] != null )
//			{
				m_nineparts[5].SetSize( m_fBorder, m_fHeight );
				m_nineparts[5].transform.localPosition = new Vector3(m_fWidth * 0.5f, 0, 0);
				m_nineparts[5].anchor = SpriteRoot.ANCHOR_METHOD.MIDDLE_LEFT;
//			}
//			if( m_nineparts[6] != null )
//			{
				m_nineparts[6].SetSize( m_fBorder, m_fBorder );
				m_nineparts[6].transform.localPosition = new Vector3(-m_fWidth * 0.5f, -m_fHeight * 0.5f, 0);
				m_nineparts[6].anchor = SpriteRoot.ANCHOR_METHOD.UPPER_RIGHT;
//			}
//			if( m_nineparts[7] != null )
//			{
				m_nineparts[7].SetSize( m_fWidth, m_fBorder );
				m_nineparts[7].transform.localPosition = new Vector3(0, -m_fHeight * 0.5f, 0);
				m_nineparts[7].anchor = SpriteRoot.ANCHOR_METHOD.UPPER_CENTER;
//			}
//			if( m_nineparts[8] != null )
//			{
				m_nineparts[8].SetSize( m_fBorder, m_fBorder );
				m_nineparts[8].transform.localPosition = new Vector3(m_fWidth * 0.5f, -m_fHeight * 0.5f, 0);
				m_nineparts[8].anchor = SpriteRoot.ANCHOR_METHOD.UPPER_LEFT;
//			}
		}
	}
}
